package com.ocom.wxapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.MapKey;
import com.ocom.common.entity.master.EntityEcardRegCode;
import com.ocom.common.request.wx.UserInfoPageOnShowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.wxapi.vo.UserInfoPageOnShowVo;
import com.ocom.common.vo.AccessListVo;
import com.ocom.common.vo.CardInfoVo;
import com.ocom.wxapi.vo.UserInfoVo;
import com.ocom.wxapi.client.AccessClientService;
import com.ocom.wxapi.client.HumanClientService;
import com.ocom.wxapi.controller.UserInfoPageController;
import com.ocom.wxapi.service.UserInfoPageService;
import com.ocom.wxapi.vo.UserInfoMapVo;
import com.ocom.wxapi.vo.UserInfoYkmVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserInfoPageServiceImpl implements UserInfoPageService {
    Logger logger= LoggerFactory.getLogger(UserInfoPageController.class);
    @Autowired
    private HumanClientService humanClientService;

    @Autowired
    AccessClientService accessClientService;
    // 用户信息（UserInfoPage）
    //2.11.1 显示页面（UserInfoPage/onShow）
    @Override
    public Result onShow(UserInfoPageOnShowRequest request) {
        UserInfoPageOnShowVo userInfoPageOnShowVo =new UserInfoPageOnShowVo();

        userInfoPageOnShowVo.setUserId(request.getUserId());

        Map<String,String> map =new HashMap<>();
        map.put("pageTitle","用户与健康码信息");
        map.put("cmdRegCode","生成注册码");
        map.put("lblAccessCaption","门禁权限");
        map.put("cmdRemoveCaption","移除人员");
        map.put("cmdDeptCaption","调整部门");
        userInfoPageOnShowVo.setPageFormat(map);

        Long comId = request.getCompanyId();
        Long pId = request.getUserId();

        Result result= humanClientService.GetPersonInfo(comId,pId);
        logger.info("PersonnelGetPersonInfo:{{}}",result);
        if (result.getCode()==200) {
            UserInfoYkmVo userInfoYkmVo = JSON.parseObject(JSON.toJSONString(result.getData()), UserInfoYkmVo.class);
            logger.info("UserInfoYkmVo:{{}}",userInfoYkmVo);
            UserInfoVo userInfoVo = new UserInfoVo();
            userInfoVo.setUserName(userInfoYkmVo.getPName());
            userInfoVo.setIdentity(userInfoYkmVo.getPIdentity());
            userInfoVo.setTelephone(userInfoYkmVo.getPPhone());
            userInfoVo.setPCode(userInfoYkmVo.getPCode());
            userInfoVo.setDeptId(userInfoYkmVo.getDeptId());
            userInfoVo.setUpdateTime(userInfoYkmVo.getUpdateTime());
            userInfoVo.setCodeColor(userInfoYkmVo.getCodeColor());
            userInfoVo.setColorTime(userInfoYkmVo.getColorTime());
            userInfoVo.setHsResult(userInfoYkmVo.getHsResult());
            userInfoVo.setHsTime(userInfoYkmVo.getHsTime());
            userInfoVo.setYmNum(userInfoYkmVo.getYmNum());
            userInfoVo.setYmTime(userInfoYkmVo.getYmTime());
            userInfoVo.setPPhotoUrl(userInfoYkmVo.getPPhotoUrl());
            userInfoPageOnShowVo.setUserInfo(userInfoVo);

        }


        userInfoPageOnShowVo.setCardOperation(0);

        CardInfoVo cardInfoVo=new CardInfoVo();



        //查找卡
        Result ecardRegCode = humanClientService.getEcardRegCode(request.getCompanyId(), request.getUserId());
        if(ecardRegCode.getCode()==200){
            EntityEcardRegCode entityEcardRegCode=  JSON.parseObject(JSON.toJSONString(ecardRegCode.getData()), EntityEcardRegCode.class);
            if(entityEcardRegCode!=null){
                cardInfoVo.setRegCode(entityEcardRegCode.getRegCode());
                cardInfoVo.setCardNo("已领取电子卡");
                userInfoPageOnShowVo.setNewRegCode(false);
            }else{
                cardInfoVo.setCardNo("未领取电子卡");
                userInfoPageOnShowVo.setNewRegCode(true);
            }
        }else{
            cardInfoVo.setCardNo("未领取电子卡");
            userInfoPageOnShowVo.setNewRegCode(true);
        }
//        cardInfoVo.setRegCode(" 注册码：1111");
//        cardInfoVo.setCardNo("已领取电子卡");
//        userInfoPageOnShowVo.setNewRegCode(false);

//        Result wxUserByPid = humanClientService.getWxUserByPid(request.getCompanyId(), request.getUserId());
//
//
//        if( wxUserByPid.getCode()== 200){
//            EntityWechatPerInfo entityWechatPerInfo=  JSON.parseObject(JSON.toJSONString(wxUserByPid.getData()), EntityWechatPerInfo.class);
//
//            if(entityWechatPerInfo!=null){
//                cardInfoVo.setCardNo("已领取电子卡");
//                userInfoPageOnShowVo.setNewRegCode(false);
//            }else{
//                cardInfoVo.setCardNo("未领取电子卡");
//                userInfoPageOnShowVo.setNewRegCode(true);
//
//                //查找卡
//                Result ecardRegCode = humanClientService.getEcardRegCode(request.getCompanyId(), request.getUserId());
//                if(ecardRegCode.getCode()==200){
//                    EntityEcardRegCode entityEcardRegCode=  JSON.parseObject(JSON.toJSONString(ecardRegCode.getData()), EntityEcardRegCode.class);
//                    if(entityEcardRegCode!=null){
//                        cardInfoVo.setRegCode(" 注册码："+entityEcardRegCode.getRegCode());
//                    }
//                }
//            }
//        }

        userInfoPageOnShowVo.setCardInfo(cardInfoVo);

        Result accessByUser = accessClientService.getAccessByUser(request.getCompanyId(), request.getUserId());
        if (accessByUser.getCode()==200) {
            List<AccessListVo> listVos=  JSON.parseObject(JSON.toJSONString(accessByUser.getData()), List.class);
            userInfoPageOnShowVo.setAccessList(listVos);
        }

        logger.info("userInfoPageOnShowVo:"+ userInfoPageOnShowVo);
        return ResultUtil.success(userInfoPageOnShowVo);
    }
}
